﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using weikebuluo.DAL;
using weikebuluo.Common;
using weikebuluo.Model;

namespace weikebuluo.web.wechat.vip
{
    public partial class oa_index : System.Web.UI.Page
    {
        Oa_top_dal topdal = new Oa_top_dal();
        protected Oa_top_model topmodel = new Oa_top_model();
        protected DataTable dttop = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            //判断是否绑定会员卡
            if (!string.IsNullOrEmpty(Request.QueryString["wxid"]))
            {
                dttop = topdal.GetList("String21='" + Request.QueryString["wxid"] + "' and Lbcode='vcardinfo'").Tables[0];
            }
            else {
                Response.Write("请微信登录！");
                Response.End();
            }

            switch (Request.QueryString["action"])
            {
                case "bindvip":
                    submit();
                    break;
            }
        }

        private void submit()
        { 
            //绑定会员卡
            dttop = topdal.GetList("string01='" + Request.Form["name"] + "' and Lbcode='vcardinfo'").Tables[0];
            if(dttop.Rows.Count == 0){
                Response.Write("{\"error\":\"此卡不存在！\"}");
                Response.End();
            }
            DataTable dttop1 = topdal.GetList("string01='" + Request.Form["name"] + "' and String02 ='" + Request.Form["phone"].Trim().ToString() + "' and  Lbcode='vcardinfo'").Tables[0];
            if (dttop1.Rows.Count == 0)
            {
                Response.Write("{\"error\":\"密码错误！\"}");
                Response.End();
            }
            if (!string.IsNullOrEmpty(dttop.Rows[0]["String21"].ToString()))
            {
                Response.Write("{\"error\":\"此卡已经绑定！\"}");
                Response.End();
            }
            topmodel = topdal.GetModel(int.Parse(dttop.Rows[0]["Uid"].ToString()));
            topmodel.String13 = Request.QueryString["uid"].ToString();
            if (Request.QueryString["wxid"].Trim().ToString() == "null")
            {
                Response.Write("{\"error\":\"请从微信登录！\"}");
                Response.End();
            }
            topmodel.String21 = Request.QueryString["wxid"].Trim().ToString();
            if (topdal.Update(topmodel))
            {
                Response.Write("{\"error\":\"200\",\"msg\":\"\",\"url\":\"index.aspx?wxid=" + Request.QueryString["wxid"] + "&uid=" + Request.QueryString["uid"] + "\"}");
                Response.End();
            }
            else
            {
                Response.Write("{\"error\":\"获得会员卡失败！\"}");
                Response.End();
            }

        }
    }
}